#include "Comm.hpp"
#include "Fifo.hpp"


int main()
{
    // 1.获取key
    key_t key = GetShmKey();
    cout << "key: "<< ToHex(key) << endl;
    //sleep(2);

    // 2.创建共享内存
    int shmid = CraetShm(key, defaultsize);
    cout << "shmid: "<< shmid << endl;
    //sleep(2);

    // 3.将共享内存和进程进程挂接
    char* addr = (char*)ShmAttach(shmid);
    cout << "Attch shm success, addr: " << ToHex((uint64_t)addr) << endl;
    //sleep(2);

    // !!!先引入管道
    Fifo fifo(Path);
    Sync syn;
    syn.OpenRead();

    // 服务端
    // 此时就可以进行通信了
    for(;;)
    {   
        if(!syn.Wait()) break;//先等待
        cout << "shm content: " << addr << endl;
    }
        
    // 4.删除映射关系
    ShmDetach(addr);
    cout << "Detach shm success, addr: " << ToHex((uint64_t)addr) << endl;
    sleep(10);

    // 5.删除共享内存
    DeleteShm(shmid);
    return 0;
}